package DAL;

import Entity.BCategory;
import Entity.Books;
import Entity.Category;
import IDAL.ReaderTypePort;
import Utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.util.List;

/**
 *张昊 java
 */
public class ReaderTypeDao implements ReaderTypePort {
    @Override
    public int addReaderType(Object... args) {
        int result = 0;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "insert into l_category(readersTypeName,days,bookNumber,fine)values(?,?,?,?)";

            result = runner.update(conn, sql, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public int updateReaderType(Object... args) {
        int result = 0;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "update l_category set " +
                    " readersTypeName = ?," +
                    " days = ?,"+
                    " bookNumber = ?," +
                    " file = ?" +
                    " where readersTypeNumber = ?";
            result = runner.update(conn, sql, args);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public int deleteReaderType(Object... args) {
        int result = 0;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "delete from l_category where readersTypeNumber=?";
            result = runner.update(conn, sql, args);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public List<Category> selectAllReaderType(Object... args) {
        List<Category> result = null;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "select * from l_category";
            BeanListHandler<Category> handler = new BeanListHandler<>(Category.class);
            result = runner.query(conn, sql, handler);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public Category selectReaderType(Object... args) {
        Category result = null;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "select * from l_category where readersTypeName = ?";
            BeanHandler<Category> handler = new BeanHandler<>(Category.class);
            result = runner.query(conn, sql, handler, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    public Category selectReaderTypeId(Object... args) {
        Category result = null;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "select * from l_category where readersTypeNumber = ?";
            BeanHandler<Category> handler = new BeanHandler<>(Category.class);
            result = runner.query(conn, sql, handler, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }
    
}
